<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span style="color:skyblue;font-size:20px">李云鹤真帅科技股份有限公司</span>
          </el-col>

        </el-row>

        <el-tree
          :data="list"
          :props="{
            children: 'children',
            label: 'label'
          }"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>
      </el-card>

      <el-dialog :title="isEdit?'编辑部门':'添加部门'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false" @close="resetForm">
        <addorEdit :id="curId" ref="addOrEditRef" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { tranDataTree } from '@/utils'
import { getDepartments, delDepartmentDetails } from '@/api/departments'
import addorEdit from './deptDialog.vue'
export default {
  components: { addorEdit },
  data() {
    return {
      showDialog: false,
      list: [],
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDeparments()
  },
  methods: {
    // 去除添加部门有数据的情况
    resetForm() {
      this.$refs.addOrEditRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('是否删除该部门?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartmentDetails(id)
        this.loadDeparments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentDetails()
      })
    },
    // 添加部门
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    // 渲染树形结构数据
    async loadDeparments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // console.log(this.originList, '------')
        res.data.depts.shift()
        this.list = tranDataTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹层 并刷新页面数据
    hSuccess() {
      this.showDialog = false
      this.loadDeparments()
    }
  }
}
</script>
